package cn.com.dcsgo.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * Spring和JDBC配置
 *
 * @author Dcsgo
 * @since 2025/2/5 15:47
 */
@Configuration
@PropertySource("classpath:jdbc.properties")
@ComponentScan({"cn.com.dcsgo.controller", "cn.com.dcsgo.service", "cn.com.dcsgo.dao"})
public class SpringConfiguration {
    @Bean
    public DruidDataSource druidDataSource(
            @Value("${mysql.url}") String url,
            @Value("${mysql.driver}") String driver,
            @Value("${mysql.username}") String username,
            @Value("${mysql.password}") String password
    ) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

}
